import logger from '../core/logger';
import { clientBase } from './core/clientBase';
import { buyInfo } from '../models/manga/buyInfo';
import axios, { AxiosError, AxiosResponse } from '@ohos/axios';

class epClient extends  clientBase {
  getBuyInfo(id: number): Promise<buyInfo | string> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`ep_id=${id}`);
        axios.post<string, AxiosResponse<string>, any>("https://manga.bilibili.com/twirp/comic.v1.Comic/GetEpisodeBuyInfo", data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data)) {
                let arr: buyInfo = response.data.data;
                resolve(arr);
              } else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  buy(id: number, type: number, cid: number, pay: number): Promise<boolean | string> {
    return new Promise(async (resolve) => {
      try {
        let data;
        switch (type) {
          case 1:
            data = `buy_method=3&ep_id=${id}&pay_amount=${pay}&auto_pay_gold_status=2`;
            break;
          case 2:
            data = `buy_method=2&auto_pay_gold_status=2&ep_id=${id}&coupon_id=${cid}`;
            break;
          case 3:
            data = `buy_method=5&ep_id=${id}`;
            break;
          case 4:
            data = `buy_method=4&auto_pay_gold_status=2&ep_id=${id}&coupon_id=${cid}`;
            break;
        }
        data = await this.createPostParam(data);
        axios.post<string, AxiosResponse<string>, any>("https://manga.bilibili.com/twirp/comic.v1.Comic/BuyEpisode", data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              if (this.isSuccess(response.data))
                resolve(true);
              else {
                let msg = this.repMessage(response.data);
                resolve(msg);
              }
            } else
              resolve(response.statusText);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(this.errMessage(err));
        });
      } catch (err) {
        logger.error(err);
        resolve('');
      }
    });
  }

  addHistory(comic_id: number, ep_id: number): Promise<boolean> {
    return new Promise(async (resolve) => {
      try {
        let data = await this.createPostParam(`comic_id=${comic_id}&ep_id=${ep_id}`);
        axios.post<string, AxiosResponse<string>, any>("https://manga.bilibili.com/twirp/bookshelf.v1.Bookshelf/AddHistory", data)
          .then((response: AxiosResponse) => {
            if (response.status == 200) {
              let ret = this.isSuccess(response.data);
              resolve(ret);
            } else
              resolve(false);
          }).catch((err: AxiosError) => {
          logger.error(err);
          resolve(false);
        });
      } catch (err) {
        logger.error(err);
        resolve(false);
      }
    });
  }
}
export default new epClient();